package com.woniuxy.creditsystem.systemmag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.DataDictionaryInfo;
import com.woniuxy.creditsystem.common.bean.PageRequest;
import com.woniuxy.creditsystem.common.bean.UserInfo;
import com.woniuxy.creditsystem.common.mapper.DataDictionaryMapper;
import com.woniuxy.creditsystem.systemmag.service.IDataDictionaryService;
import com.woniuxy.creditsystem.systemmag.util.ImportExcel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Transactional(readOnly = true)
@Service
public class DataDictionaryServiceImpl implements IDataDictionaryService {

    @Resource
    private DataDictionaryMapper dataDictionaryMapper;

    @Override
    public List<DataDictionaryInfo> findByDicType(String dicType) {
        return dataDictionaryMapper.findByDicType(dicType);
    }

    @Override
    public PageInfo<DataDictionaryInfo> findAllByParams(PageRequest request, DataDictionaryInfo data) {
        return PageHelper.startPage(request.getPageNum(),request.getLimit()).
                doSelectPageInfo(() -> dataDictionaryMapper.findAllByParams(request,data));
    }

    /**
     * 新增数据
     * */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int save(DataDictionaryInfo data) {
        data.setNumber(System.currentTimeMillis());
        //从Session中获取操作用户
        Subject subject =  SecurityUtils.getSubject();
        Session session = subject.getSession();
        UserInfo userInfo = (UserInfo) session.getAttribute("user");
        data.setEditPerson(userInfo.getUserName());
        return dataDictionaryMapper.save(data);
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int update(DataDictionaryInfo data) {
        return dataDictionaryMapper.update(data);
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int delete(long id) {
        return dataDictionaryMapper.delete(id);
    }

    /**
     * 从Excel中导入数据
     * @Param excel对象文件输入流
     * */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int saveList(MultipartFile file){
        List<DataDictionaryInfo> lists = null;
        try {
             lists = ImportExcel.getList(file);
        }catch (Exception e){
            e.printStackTrace();
        }
        for(DataDictionaryInfo data : lists){
            dataDictionaryMapper.save(data);
        }
        return 0;
    }
}
